//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ruoyi.web.controller.user;

import com.ruoyi.live.app.service.CacheService;
import com.ruoyi.live.base.domain.Task;
import com.ruoyi.live.base.domain.TaskCatalog;
import com.ruoyi.live.base.domain.TaskSource;
import com.ruoyi.live.base.domain.User;
import com.ruoyi.live.base.mapper.LiveExamMapper;
import com.ruoyi.live.base.mapper.TaskUserLearnLogMapper;
import com.ruoyi.live.base.mapper.UserMapper;
import com.ruoyi.live.configuration.SessionInfo;
import com.ruoyi.live.tool.commons.ICommons;
import com.ruoyi.live.tool.utils.Message;
import com.ruoyi.live.user.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import jakarta.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping({"/student"})
public class StudentController {
    @Autowired
    UserMapper userDao;
    @Autowired
    StudentService service;
    @Autowired
    CacheService cacheService;
    @Autowired
    TaskUserLearnLogMapper logDao;
    @Autowired
    LiveExamMapper examDao;

    public StudentController() {
    }

    @RequestMapping({"/updatePsd"})
    public String changePsd() {
        return "/common/user_update_psd";
    }

    @RequestMapping({"/editUser"})
    public ModelAndView editUser() {
        User user = SessionInfo.getUser();
        if (ICommons.isNullOrEmpty(user.getUserHeaderImg())) {
            user.setUserHeaderImg("pic_tea.png");
        }

        return (new ModelAndView("/common/user_edit_form")).addObject("entity", user);
    }

    @RequestMapping(
            value = {"/getHeaderImg/{name}"},
            produces = {"image/jpeg"}
    )
    @ResponseBody
    public byte[] getImage(@PathVariable String name, HttpServletRequest request) throws IOException {
        String filePath = request.getServletContext().getRealPath("static");
        FileInputStream inputStream = new FileInputStream(new File(filePath + "/upload/" + name));
        byte[] bytes = new byte[inputStream.available()];
        inputStream.read(bytes, 0, inputStream.available());
        return bytes;
    }

    @RequestMapping({"/learnAnalyse"})
    public ModelAndView learnAnalyse() {
        return (new ModelAndView("/student/student_fx_detail")).addObject("datas", this.service.getLearnAnalyse()).addObject("id", SessionInfo.getUserId());
    }

    @RequestMapping({"/analyse/bar"})
    @ResponseBody
    public Map<String, Object> analyseBar(Integer type, Long catalogId) {
        return this.service.analyseBar(type, catalogId);
    }

    @ResponseBody
    @RequestMapping({"/updateUserInfo"})
    public Message updateUserInfo(User user) {
        try {
            this.userDao.updateById(user);
            return new Message(true, "更新成功");
        } catch (Exception var3) {
            var3.printStackTrace();
            return new Message(false, "更新失败");
        }
    }

    @RequestMapping({"/center"})
    public ModelAndView center() {
        return (new ModelAndView("/student/student_center")).addObject("maxs", this.service.getMaxs()).addObject("avgs", this.service.getAvgs()).addObject("tasks", this.service.getNoCompleteTasks());
    }

    @ResponseBody
    @RequestMapping({"/updateHeaderImg"})
    public Message upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        String fileName = file.getOriginalFilename();
        if (ICommons.isNullOrEmpty(fileName)) {
            return new Message(false, "文件名不能为空");
        } else {
            if (fileName.indexOf("\\") != -1) {
                fileName = fileName.substring(fileName.lastIndexOf("\\"));
            }

            String ext = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
            if (ICommons.isNullOrEmpty(ext)) {
                return new Message(false, "扩展名不能为空");
            } else if (!".jpg.png".contains(ext)) {
                return new Message(false, "请上传png或jpg文件");
            } else {
                String filePath = request.getServletContext().getRealPath("/static/static/upload/");
                File targetFile = new File(filePath);
                if (!targetFile.exists()) {
                    targetFile.mkdirs();
                }

                FileOutputStream out = null;
                String name = UUID.randomUUID().toString().replaceAll("-", "") + ext;

                label95: {
                    Message var10;
                    try {
                        out = new FileOutputStream(filePath + name);
                        out.write(file.getBytes());
                        break label95;
                    } catch (Exception var20) {
                        var20.printStackTrace();
                        var10 = new Message(false, "上传失败");
                    } finally {
                        try {
                            out.flush();
                            out.close();
                        } catch (Exception var19) {
                            var19.printStackTrace();
                        }

                    }

                    return var10;
                }

                SessionInfo.getUser().setUserHeaderImg(name);
                return new Message(true, name);
            }
        }
    }

    @ResponseBody
    @RequestMapping({"/updatePsdSave"})
    public Message updatePsdSave(String userpass, String userpassnew) {
        if (!ICommons.isNullOrEmpty(userpass) && !ICommons.isNullOrEmpty(userpassnew)) {
            try {
                return this.service.updatePsdSave(userpass, userpassnew);
            } catch (Exception var4) {
                var4.printStackTrace();
                return new Message(false, "修改失败");
            }
        } else {
            return new Message(false, "输入有误");
        }
    }

    @RequestMapping({"/listStudentTask"})
    public ModelAndView listTeacherTask(@Nullable Task param) {
        List<TaskCatalog> list = null;
        if (param == null) {
            list = this.service.getTasks();
        } else if (ICommons.isNullOrEmpty(param.getState()) && ICommons.isNullOrEmpty(param.getPlatformId()) && ICommons.isNullOrEmpty(param.getTaskName())) {
            list = this.service.getTasks();
        } else {
            if (!ICommons.isNullOrEmpty(param.getTaskName()) && param.getTaskName().contains(";")) {
                param.setTaskName(param.getTaskName().replaceAll(";", ""));
            }

            list = this.service.listTasksByParam(param);
        }

        return (new ModelAndView("/student/student_task_list")).addObject("list", list);
    }

    @RequestMapping({"/task/index/{id}"})
    public ModelAndView taskIndex(@PathVariable Long id) {
        this.service.insertLog(id);
        return (new ModelAndView("/student/task_index")).addObject("id", id).addObject("sources", this.service.getSourceParams(id)).addObject("headerImg", SessionInfo.getUser().getUserHeaderImg());
    }

    @RequestMapping({"/openSource/{id}"})
    public ModelAndView openSource(@PathVariable Long id) {
        return this.service.openSource(id);
    }

    @RequestMapping({"/openTask/{id}"})
    public ModelAndView openTask(@PathVariable Long id) {
        return (new ModelAndView("/task/preview/preview_task")).addObject("task", this.cacheService.getTaskDetail(id));
    }

    @RequestMapping({"/toSourceList"})
    public ModelAndView toSourceList(Long id) {
        return new ModelAndView("/task/preview/preview_source");
    }

    @ResponseBody
    @RequestMapping({"/getSourceList/{id}"})
    public List<TaskSource> getSourceList(@PathVariable Long id) {
        return this.service.getSourceList(id);
    }

    @RequestMapping({"/returnList"})
    public ModelAndView returnList() {
        return new ModelAndView("redirect:/index?type=task");
    }

    @RequestMapping({"/tj/top"})
    @ResponseBody
    public Map<String, Object> top() {
        return this.service.getDatas();
    }

    @RequestMapping({"/tj/bar"})
    @ResponseBody
    public Map<String, Object> bar() {
        return this.service.getBarDatas();
    }
}
